package clientmain.auth.header;

import java.util.List;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
	private String userId;
	private String password;

	public AuthInterceptor(String userId, String password) {
		super(Phase.PREPARE_SEND);
		this.userId = userId;
		this.password = password;
	}

	@Override
	public void handleMessage(SoapMessage msg) throws Fault {
		List<Header> headers = msg.getHeaders();
		
		Document doc = DOMUtils.createDocument();
		Element ele = doc.createElement("authHead");
		Element idEle = doc.createElement("userId");
		idEle.setTextContent(userId);
		Element pwdEle = doc.createElement("password");
		pwdEle.setTextContent(password);
		
		ele.appendChild(idEle);
		ele.appendChild(pwdEle);
		headers.add(new Header(new QName("hy"), ele));
	}

}
